using UnityEngine;
using System.Collections;

public class Bird : MonoBehaviour 
{
	public float speed = 5;
	private SantaClaus player;
    public Texture2D[] textures = new Texture2D[3];
    public float time;
    private int i = 0;

	void Start()
	{
        try { player = GameObject.FindGameObjectWithTag("Player").GetComponent<SantaClaus>(); }
        catch { }

        StartCoroutine("Animate");
	}
	
	void Update() 
	{	
		transform.Translate(-speed*Time.deltaTime,0,0);
		
		if(transform.position.x <= -15)
		{
			Destroy(this.gameObject);
		}
	}

    IEnumerator Animate()
    {
        renderer.material.mainTexture = textures[i];
        yield return new WaitForSeconds(time);
        i++;

        if(i == 3)
        {
            i = 0;
        }

        StartCoroutine("Animate");
    }
	
	void OnTriggerEnter(Collider other)
	{
		if(other.gameObject.tag == "Gift")
		{
			Destroy(other.gameObject);
            PlayerInformations.AddPoints(-50);
		}
		
		if(other.gameObject.tag == "Player")
		{
            if(player.IsInvencible() == false)
                player.Die();
		}
	}
}